<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item label="手机号" prop="agentMobile">
        <el-input
          v-model="queryParams.agentMobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理商名称" prop="agentName">
        <el-input
          v-model="queryParams.agentName"
          placeholder="请输入代理商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cancel:agentLevel:add']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="whiteList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="等级名称" align="center" prop="agentTitle" />
      <el-table-column label="等级" align="center" prop="agentLevel" />
      <el-table-column label="分润比例" align="center" prop="shareRatio" />
      <el-table-column label="代理类型" align="center" prop="agentType">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.agentType == 1">合伙人</el-tag>
          <el-tag v-if="scope.row.agentType == 2">代理商</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="升级金额" align="center" prop="upAmount" />
      <el-table-column label="类型" align="center" prop="upType">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.upType == 1">自购</el-tag>
          <el-tag v-if="scope.row.upType == 2">推广</el-tag>
          <el-tag type="warning" v-if="scope.row.upType == 3"
            >自购或推广</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cancel:agentLevel:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改后台导入用户白名单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="等级名称" prop="agentTitle">
          <el-input v-model="form.agentTitle" placeholder="请输入等级名称" />
        </el-form-item>
        <el-form-item label="等级" prop="agentLevel">
          <el-input
            v-model="form.agentLevel"
            placeholder="请输入等级"
            type="number"
          />
        </el-form-item>
        <el-form-item label="分润比例" prop="shareRatio">
          <el-input
            v-model="form.shareRatio"
            placeholder="请输入分润比例"
            type="number"
          >
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="代理类型" prop="agentType">
          <el-radio-group v-model="form.agentType">
            <el-radio :label="1">合伙人</el-radio>
            <el-radio :label="2">代理商</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="升级金额" prop="upAmount">
          <el-input
            v-model="form.upAmount"
            placeholder="请输入升级金额"
            type="number"
          />
        </el-form-item>
        <el-form-item label="类型" prop="upType">
          <el-radio-group v-model="form.upType">
            <el-radio :label="1">自购</el-radio>
            <el-radio :label="2">推广</el-radio>
            <el-radio :label="3">自购或推广</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAgentList,
  addAgentLevel,
  detailLevel,
  editAgentLevel,
} from "@/api/system/agent";
import { getToken } from "@/utils/auth";
export default {
  name: "White",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 后台导入用户白名单表格数据
      whiteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCode: false,
      titleCode: "",
      xcxImgUrl: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mobile: null,
        userName: null,
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/cancel/userWhite/importDataVir",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询后台导入用户白名单列表 */
    getList() {
      this.loading = true;
      getAgentList(this.queryParams).then((response) => {
        this.whiteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        agentMobile: null,
        agentName: null,
        level: 1,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //小程序
    handleCode(row) {
      console.log(row);
      this.openCode = true;
      this.xcxImgUrl = row.xcxImgUrl;
      this.titleCode = "代理人" + row.agentName + "的小程序码";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加代理人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const { id } = row;
      detailLevel(id).then((res) => {
        this.form = res.data;
        this.open = true;
      });
    },
    // 购买详情的跳转
    handleOrder(row) {
      console.log(row);
      this.$router.push({
        path: "/agent/agentOrder",
        query: { param: row.id },
      });
    },
    // 申请提现记录的跳转
    handleWithdraw(row) {
      console.log(row);
      this.$router.push({
        path: "/agent/agentwithDrawLog",
        query: { param: row.id },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            editAgentLevel(this.form).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAgentLevel(this.form).then((response) => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除后台导入用户白名单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWhite(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "/cancel/userWhite/importTemplate",
        {},
        `white_template_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/white/export",
        {
          ...this.queryParams,
        },
        `white_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
